package com.hgd.security.evp.biz.rest;

import com.hgd.security.evp.biz.biz.EvpDepartBiz;
import com.hgd.security.evp.biz.entity.EvpDepart;
import com.hgd.security.evp.biz.vo.DeptTree;
import com.hgd.security.evp.biz.vo.EvpDepartVo;
import com.hgd.security.evp.common.util.EmptyUtil;
import com.hgd.security.evp.common.util.ListResult;
import com.hgd.security.evp.common.util.ObjectResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("eDepart")
@Log4j
@Api(tags = "部门管理")
public class EDepartController {

    @Autowired
    private EvpDepartBiz evpDepartBiz;

    @PostMapping("/instance")
    @ApiOperation("添加部门")
    public ObjectResultResponse<Void> addDepart(@RequestBody @ApiParam(value = "待添加对象") EvpDepart eDepart,
        BindingResult bindingResult) {
        log.info("/eDepart/instance==>添加部门");
        ObjectResultResponse<Void> oResult = new ObjectResultResponse<>();
        if (bindingResult.hasErrors()) {
            oResult.setStatus(400);
            oResult.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return oResult;
        }

        oResult = evpDepartBiz.addEvpDepart(eDepart);
        return oResult;
    }

    @GetMapping("/tree")
    @ApiOperation("获取部门树")
    public List<DeptTree> eDepartTree() {
        log.info("/eDepart/tree==>获取部门树");
        return evpDepartBiz.getDepartTree();
    }

    @GetMapping("/list")
    @ApiOperation("分页获取列表")
    public ListResult<EvpDepartVo> getDeptList(@RequestParam(value = "evpName", defaultValue = "") String evpName,
        @RequestParam(value = "evpCode", defaultValue = "") @ApiParam("部门CODE") String evpCode,
        @RequestParam(value = "evpType", defaultValue = "") @ApiParam("部门类型") String evpType,
        @RequestParam(value = "page", defaultValue = "1") @ApiParam("当前页，默认为1") Integer page,
        @RequestParam(value = "rows", defaultValue = "10") @ApiParam("页容量，默认为10") Integer rows) {
        EvpDepart depart = new EvpDepart();
        depart.setEvpName(evpName);
        depart.setEvpCode(evpCode);
        depart.setEvpType(evpType);

        return evpDepartBiz.getDeptListBindParent(depart, page, rows);
    }

    @GetMapping("/list/combobox")
    @ApiOperation("分页获取用户列表")
    public List<EvpDepart> getUserListCombobox(@RequestParam(value = "evpName", defaultValue = "") String evpName,
        @RequestParam(value = "evpCode", defaultValue = "") @ApiParam("部门CODE") String evpCode,
        @RequestParam(value = "evpType", defaultValue = "") @ApiParam("部门类型") String evpType,
        @RequestParam(value = "page", defaultValue = "1") @ApiParam("当前页，默认为1") Integer page,
        @RequestParam(value = "rows", defaultValue = "10") @ApiParam("页容量，默认为10") Integer rows) {
        EvpDepart depart = new EvpDepart();
        depart.setEvpName(evpName);
        depart.setEvpCode(evpCode);
        depart.setEvpType(evpType);

        ListResult<EvpDepart> userLiset = evpDepartBiz.getDeptList(depart, page, rows);
        if (EmptyUtil.isNotEmpty(userLiset)) {
            return userLiset.getRows();
        }
        return new ArrayList<>();
    }

    @GetMapping("/instance/{id}")
    @ApiOperation("获取单个对象")
    public EvpDepartVo getDeptById(@PathVariable(value = "id") @ApiParam("待获取对象ID") Integer id) {
        return evpDepartBiz.getDeptById(id);
    }

    @PutMapping("/instance")
    @ApiOperation("更新部门")
    public ObjectResultResponse<Void> updateDepart(@RequestBody @ApiParam(value = "待更新对象") EvpDepart eDepart,
        BindingResult bindingResult) {
        log.info("/eDepart/instance==>更新部门");
        ObjectResultResponse<Void> oResult = new ObjectResultResponse<>();
        if (bindingResult.hasErrors()) {
            oResult.setStatus(400);
            oResult.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return oResult;
        }

        oResult = evpDepartBiz.updateEDepart(eDepart);
        return oResult;
    }

    @DeleteMapping("/instance/{id}")
    @ApiOperation("删除单个对象")
    public ObjectResultResponse<Void> delDeptById(@PathVariable(value = "id") @ApiParam(value = "待删除对象ID") Integer id) {
        return evpDepartBiz.delDeptById(id);
    }
}